<template>
	<view class="u-p-30">
		<view class="u-bg-fff u-r-20 u-p-20 u-m-b-20" v-for="(item,index) in list" @click="choose(item)">
			<view class="u-f-28" style="padding: 20rpx 0;">
				{{item.username}}  <text class="u-m-l-20"> {{item.mobile}}</text>
			</view>
			<view class="u-flex">
				<image src="../../../static/address3.png" style="width: 24rpx;height: 24rpx;margin-top: 10rpx;"></image>
				<view class="u-flex-1 u-f-26 u-m-l-10" style="color: #999;">{{item.area}}{{item.address}}</view>
			</view>
			<u-line margin="32rpx 0 20rpx 0"></u-line>
			<view class="u-flex-y-center u-flex-between">
				<view class="u-flex-y-center" @click.stop="change(item)">

					<image src="../../../static/checked.png" style="width: 30rpx;height: 30rpx;"
											v-if="item.is_default==1"></image>
										<image src="../../../static/check.png" style="width: 30rpx;height: 30rpx;" v-else></image>
					<view class="u-f-24 u-m-l-14">设为默认地址</view>
				</view>
				<view class="u-flex-y-center">
					<view class="u-f-24 u-p-l-40" style="color: #999;" @click.stop="delItem(item.id)">删除</view>
					<view class="u-f-24 u-p-l-40" style="color: #999;"  @click.stop="$u.route('/pages/member/address/addressDetail?id='+item.id)">编辑</view>
				</view>
			</view>
		</view>
		<u-gap height="120rpx"></u-gap>
		<view class="bottom">
			<view class="appBtn " style="width: 100%;" @click="$u.route('/pages/member/address/addressDetail')">+添加地址</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				page: 1,
				type: 0
			}
		},
		onShow() {
			this.list = []
			this.loadData()
		},
		onLoad(options) {
			this.type = options.type
		},
		
		methods: {
			choose(item) {
				if (this.type == 1) {
					this.$prePage.prePage().address = item;
					this.$prePage.prePage().address_id = item.id;
					uni.navigateBack()
				}else{
					uni.$u.route('/pages/member/address/addressDetail',{id:item.id})
				}
		
			},
			async change(info) {
				let is_default = 0
				if (info.is_default==0) {
					is_default = 1
				}
				await uni.$u.http.post('/User/addressSet', {
					address_id: info.id,
					is_default
				})
				uni.$u.toast('操作成功')
				this.loadData()
			},
			async loadData() {
				const res = await uni.$u.http.post('/User/address')
				this.list = res
			},
			delItem(id) {
				var _this = this
				uni.showModal({
					title: '是否删除地址',
					async success(result) {
						if (result.confirm) {
							await uni.$u.http.post('/User/addressDel', {
								address_id: id
							})
							uni.$u.toast('操作成功')
							_this.loadData()
						}
					}
				})
			}
		}
	}
</script>


<style lang="scss">
	page{
		background-color: #f4f4f4;
	}
	.bottom {
		padding: 10rpx 30rpx;
		bottom: 0;
		left: 0;
		position: fixed;
		width: 100%;
		height: 108rpx;
		background-color: #fff;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-radius: 30rpx 30rpx 0 0;
	
		.btn {
			width: 260rpx;
			height: 88rpx;
			background: linear-gradient(180deg, #FF9D00 0%, #FFBF09 100%);
			border-radius: 44rpx 0rpx 0rpx 44rpx;
			text-align: center;
			line-height: 88rpx;
			font-size: 28rpx;
			color: #fff;
		}
	
		.btn1 {
			width: 260rpx;
			height: 88rpx;
			background: linear-gradient(180deg, #209920 0%, #AAD777 100%);
			border-radius: 0rpx 44rpx 44rpx 0rpx;
			text-align: center;
			line-height: 88rpx;
			font-size: 28rpx;
			color: #fff;
		}
	}
</style>
